﻿// -----------------------------------------------------------------------
// <copyright file="AlienShip1.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace SpaceshipDuel
{
    /// <summary>
    /// Alienraumschiff Klasse 1
    /// </summary>
    public class AlienShip1 : AlienShip
    {
        #region Private fields

        Vector2 nearestObstacle;    // Koordinaten des nächstgelegenen Objektes / Hindernisses
        float maxSpeed = 3.5f;      // maximale Geschwindigkeit

        #endregion Private fields

        #region Construction

        // Konstruktor
        public AlienShip1(TextureData newShipTextureData, TextureData newShotTextureData, Vector2 newCenter, int newWorldSize, ref List<Bullet> newBullets)
        {
            textureData = newShipTextureData;
            shotTextureData = newShotTextureData;
            center = newCenter;
            rotationAngle = 0f;
            worldSize = newWorldSize;
            velocity = new Vector2(0, 0);  // Richtungsvektor der aktuellen Ausrichtung setzen
            bullets = newBullets;
            bulletSpeed = 0.8f;
            maxBulletDistance = 2000;
            shieldPower = 50;
            maxShieldPower = 100;
            rotation = 0.020f;          // Rotationsgeschwindigkeit
            speed = maxSpeed;           // Fluggeschwindigkeit
            nextBulletInterval = 0.4f;
            nextBulletTimer = new WaitTimer(nextBulletInterval);

            nearestObstacle = new Vector2(-1000, -1000);
        }

        #endregion Construction

        #region Public properties

        public Vector2 NearestObstacle
        {
            set { nearestObstacle = value; }
        }

        #endregion Public properties

        #region Public methods

        /// <summary>
        /// Update-Methode
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            velocity.X = (float)(Math.Sin(rotationAngle) * speed);
            velocity.Y = (float)(-Math.Cos(rotationAngle) * speed);
            center += velocity;
            rotationAngle += rotation;
            rotationAngle = (float)(rotationAngle % (Math.PI * 2));

            Evasion(gameTime);

            base.CheckPosition();
        }

        #endregion Public methods

        #region Private methods

        /// <summary>
        /// Schießen
        /// </summary>
        /// <param name="gameTime">Zeit</param>
        private void Shoot(GameTime gameTime)
        {
            if (nextBulletTimer.IsElapsed(gameTime.ElapsedGameTime.TotalSeconds) == true)
            {
                Vector2 bulletDirection = new Vector2((float)Math.Sin(rotationAngle), (float)-Math.Cos(rotationAngle));
                bulletDirection.Normalize();
                Vector2 bulletPosition = center + bulletDirection * 32;
                Bullet bullet = new Bullet(shotTextureData, bulletPosition, this.worldSize, this.speed + bulletSpeed, this.rotationAngle, bulletDirection, maxBulletDistance);
                bullets.Add(bullet);
                //SoundProvider.SfxLaserEnemy.Play();
            }
        }

        /// <summary>
        /// Ausweichen
        /// </summary>
        /// <param name="gameTime">Zeit</param>
        private void Evasion(GameTime gameTime)
        {
            speed = maxSpeed;

            double distance = CollisionHelper.GetDistance(center, nearestObstacle, worldSize);

            if (distance < 2000)
            {
                // Erzeuge virtuelle Koordinaten zur Zielerfassung
                Vector2 newTarget = nearestObstacle;
                Vector2 newCenter = center;

                float resultX = (float)Math.Abs(nearestObstacle.X - center.X);
                if (resultX > worldSize / 2)
                {
                    newTarget.X = (nearestObstacle.X + worldSize / 2) % worldSize;
                    newCenter.X = (center.X + worldSize / 2) % worldSize;
                }

                float resultY = (float)Math.Abs(nearestObstacle.Y - center.Y);
                if (resultY > worldSize / 2)
                {
                    newTarget.Y = (nearestObstacle.Y + worldSize / 2) % worldSize;
                    newCenter.Y = (center.Y + worldSize / 2) % worldSize;
                }

                double rotAngle = (Math.Atan2(newTarget.Y - newCenter.Y, newTarget.X - newCenter.X) + Math.PI / 2);

                double diffAngle = rotAngle - rotationAngle;

                if (Math.Abs(diffAngle) <= Math.PI / 3)
                    Shoot(gameTime);

                if (diffAngle < 800)
                {
                    // Winkelunterschied kleiner gleich 20°
                    if (Math.Abs(diffAngle) <= Math.PI / 3)
                    {
                        if (diffAngle < 0)
                        {
                            if (distance < 100)
                            {
                                rotation = (float)(Math.PI / 9);
                                speed = maxSpeed / V3;
                            }
                            else if (distance < 200)
                                rotation = (float)(Math.PI / 18);
                            else if (distance < 300)
                                rotation = (float)(Math.PI / 27);
                            else if (distance < 400)
                                rotation = (float)(Math.PI / 36);
                            else if (distance < 800)
                                rotation = (float)(Math.PI / 72);
                        }
                        else
                        {
                            if (distance < 100)
                            {
                                rotation = (float)(-Math.PI / 9);
                                speed = maxSpeed / V3;
                            }
                            else if (distance < 200)
                                rotation = (float)(-Math.PI / 18);
                            else if (distance < 300)
                                rotation = (float)(-Math.PI / 27);
                            else if (distance < 400)
                                rotation = (float)(-Math.PI / 36);
                            else if (distance < 800)
                                rotation = (float)(-Math.PI / 72);
                        }
                    }
                    else
                        rotation = 0.0f;
                }
            }
        }

        #endregion Private methods
    }
}
